import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login.vue')
  },
  {
    path: '/',
    component: MainLayout,
    children: [
      { path: '', redirect: '/home' },
      { path: 'home', name: 'Home', component: () => import('@/views/Home.vue') },
      { path: 'candidates', name: 'Candidates', component: () => import('@/views/Candidates.vue') },
      { path: 'question', name: 'Question', component: () => import('@/views/question/Question.vue') },
      { 
        path: 'question/questionCategory', 
        name: 'QuestionCategory', 
        component: () => import('@/views/question/QuestionCategory.vue')
      },
      { 
        path: 'question/questionSet', 
        name: 'QuestionSet', 
        component: () => import('@/views/question/QuestionSet.vue')
      },
      { 
        path: 'question/question', 
        name: 'QuestionList', 
        component: () => import('@/views/question/Question.vue')
      },
      { path: 'interviewRecord', name: 'InterviewRecord', component: () => import('@/views/InterviewRecord.vue') },
      { path: 'schedule', name: 'Schedule', component: () => import('@/views/Schedule.vue') },
      { path: 'positions', name: 'Positions', component: () => import('@/views/Positions.vue') },
      { path: 'interviewer', name: 'Interviewer', component: () => import('@/views/Interviewer.vue') },
      { path: 'setting', name: 'Setting', component: () => import('@/views/Setting.vue') }
    ]
  },
  {
    path: '/ai',
    component: MainLayout,
    redirect: '/ai/chat',
    name: 'AI',
    meta: { title: 'AI助手', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'chat',
        name: 'AIChat',
        component: () => import('@/views/ai/chat/index.vue'),
        meta: { title: 'AI聊天', icon: 'el-icon-chat-dot-round' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router 